<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'type'=>intval($_RQ['type']));
	if($_RQ['id']){
		$params['xueshengid']=intval($_RQ['id']);
		$return['params']['id'] =intval($_RQ['id']);
	}
	if($this->session('classid')){
		$params['classid']=$this->session('classid');
		$return['params']['classid']=$this->session('classid');
	}
	if($_RQ['title']){
		$params2=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
		$xueshengs=pdo_getall('jy_yxw_xuesheng',$params2,array('id'),'id');
		if($xueshengs){
			$xueshengs=array_keys($xueshengs);
		}else{
			$xueshengs=-1;
		}
		$params['xueshengid']=$xueshengs;
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_yxw_fangtan_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('xuesheng')->member_show($item['xueshengid']);
			$info=pdo_get('jy_yxw_xuesheng',array('id'=>$item['xueshengid'],'uniacid'=>$_SESSION['uniacid']));
			$item['classid']=$info['classid'];
			$item['title']=$info['title'];
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'售后访谈记录'),
			//array('op'=>'edit','title'=>'添加访谈记录'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'classid','title'=>'所属售后群','type'=>'select','options'=>'class'),
			array('name'=>'workerid','title'=>'访谈员工','type'=>'select','options'=>'workers'),
			array('name'=>'type','title'=>'访谈类型','type'=>'radio','options'=>'fangtan_type'),
			array('type'=>'string','title'=>'访谈内容','name'=>'remark'),
			array('name'=>'createtime','title'=>'日期','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','action'=>'/xuesheng.fangtan/edit/','title'=>'编辑'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'售后姓名','type'=>'string'),
		),
	);
	$return['class']=$this->getall('class',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['workers']=$this->getall('worker',array('role'=>array(9,10,11,12,13),'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['htmls']=$htmls;
	$return['fangtan_type']=$this->fangtan_type;
	exi($return);
}
if($_RQ['op']=='edit' || $_RQ['op']=='add'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$data=array(
			'type'=>intval($post['type']),
			'remark'=>$post['remark'],
		);
		if($id){
			pdo_update('jy_yxw_fangtan_log',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}else{
			if(!$post['xueshengid']){
				exi('请选择售后！','error');
			}
			$data['uniacid']=$_SESSION['uniacid'];
			$data['workerid']=$_SESSION['workerid'];
			$data['storeid']=$_SESSION['storeid'];
			$data['xueshengid']=$post['xueshengid'];
			$data['classid']=$this->session('classid');
			$data['createtime']=time();
			pdo_insert('jy_yxw_fangtan_log',$data);
		}
		exi('操作成功！','','goback');
	}
	if($_RQ['id']){
		if($_RQ['op']=='add'){
			$item=pdo_get('jy_yxw_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
			$item['xueshengid']=$item['id'];
			unset($item['id']);
		}else{
			$item=pdo_get('jy_yxw_fangtan_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		}
	}else{
		$item=array();
	}
	$item['type']=$item['type']?:0;
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'访谈内容',
	);
	$htmls['edit']=array();
	if(!$item['xueshengid']){
		$htmls['edit'][]=array('type'=>'choose_title','action'=>'/get_xuesheng/','name'=>'xueshengid','title'=>'选择售后');
	}
	$htmls['edit'][]=array('name'=>'type','title'=>'访谈类型','type'=>'radio','options'=>'fangtan_type');
	$htmls['edit'][]=array('type'=>'text','title'=>'访谈内容','name'=>'remark');
	$return['fangtan_type']=$this->fangtan_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['id']){
		$params['xueshengid']=intval($_RQ['id']);
		$return['params']['id'] =intval($_RQ['id']);
	}
	$params['listid']=$this->session('listid');
	if($_RQ['title']){
		$params2=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
		$xueshengs=pdo_getall('jy_yxw_xuesheng',$params2,array('id'),'id');
		if($xueshengs){
			$xueshengs=array_keys($xueshengs);
		}else{
			$xueshengs=-1;
		}
		$params['xueshengid']=$xueshengs;
	}
	$list=pdo_getall('jy_yxw_fangtan_log',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('xuesheng')->member_show($item['xueshengid']);
			$info=pdo_get('jy_yxw_xuesheng',array('id'=>$item['xueshengid'],'uniacid'=>$_SESSION['uniacid']));
			$item['title']=$info['title'];
			$item['classtitle']=pdo_getcolumn('jy_yxw_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['classid']),'title');
			$item['worker']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['workerid']),'title');
			$item['type']=$this->fangtan_type[$item['type']];
		}
	}
	$header=array(
		'title'=>'售后姓名',
		'classtitle'=>'售后群',
		'worker'=>'访谈员工',
		'type'=>'访谈类型',
		'remark'=>'访谈内容',
		'createtime'=>'日期',
	);
	model('core/tools')->export2excel($header,$list,'访谈表');
}